package com.ellricky.baselib.ui;

import android.animation.ObjectAnimator;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andr.common.http.callback.EntityCallback;
import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.StrUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.adapter.CommonGridItemDecoration;
import com.ellricky.baselib.adapter.ErCodeActAdapter;
import com.ellricky.baselib.cache.LocalCacheUtil;
import com.ellricky.baselib.entity.ErCodeActEntity;
import com.ellricky.baselib.entity.ErCodeEntity;
import com.ellricky.baselib.entity.IdNameEntity;
import com.ellricky.baselib.http.Api;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.http.callback.ECallback;
import com.ellricky.baselib.ui.clue.AddClueActivity;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.GlideUtil;
import com.ellricky.baselib.util.NewECallback;
import com.flyco.roundview.RoundRelativeLayout;
import com.google.zxing.QrCodeUtil;

import java.util.HashMap;
import java.util.Map;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ErCodeActivity extends BaseToolBarActivity<Void, BasePresenter<Void>> implements AdapterView.OnItemClickListener {

    private ImageView ivBg;
    private RoundRelativeLayout flCode;
    private ImageView ivCode;
    private TextView tvCode;
    private RelativeLayout rlAct;
    private RecyclerView rvAct;

    private ErCodeActAdapter adapter;


    @Override
    public int getContentLayout() {
        return R.layout.base_activity_er_code;
    }

    @Override
    protected void initUIView() {
        super.initUIView();
        mTvTitle.setText(R.string.base_er_code);
        if (LocalCacheUtil.isTcRole() || LocalCacheUtil.isPresidentRole() || LocalCacheUtil.isTmRole()) {
            setRightBtn(0);
        }

        ivBg = findViewById(R.id.ivBg);

        flCode = findViewById(R.id.flCode);
        ivCode = findViewById(R.id.ivCode);
        tvCode = findViewById(R.id.tvCode);

        rlAct = findViewById(R.id.rlAct);

        rvAct = findViewById(R.id.rvAct);
        rvAct.setLayoutManager(new GridLayoutManager(this, 3));
        rvAct.addItemDecoration(new CommonGridItemDecoration(this, 3, 12));
        adapter = new ErCodeActAdapter(this);
        adapter.setOnItemClickListener(this);
        rvAct.setAdapter(adapter);

        loadCode();
    }


    private void loadCode() {
        String url = EAppUtil.getApiHost();
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("action", "empEWM");
        paramMap.put("eid", LocalCacheUtil.getUid());

        HttpUtils.getInstance().postFrom(url, paramMap, new NewECallback<ErCodeEntity>(this) {

            @Override
            public void onSuccess(ErCodeEntity data, String msg) {
                flCode.setVisibility(View.VISIBLE);
                tvCode.setText(String.format("身份码：%s", data.getCode()));

                String cardNoUrl = data.getEwm_img();

                Bitmap bitmap = QrCodeUtil.createQRCode(cardNoUrl, 500);
                ivCode.setImageBitmap(bitmap);
            }
        });
    }

    @Override
    protected void onClickBtnRight() {
        super.onClickBtnRight();
        if (LocalCacheUtil.isMarketRole())
            onClickBtnRightLeft();
        else
            EAppUtil.jumpPageWithNoParam(this, AddClueActivity.class);
    }

    @Override
    protected void onClickBtnRightLeft() {
        super.onClickBtnRightLeft();
        if (rvAct.isShown()) hideAnim();
        else showAnim();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        adapter.onClickItem(position);
        hideAnim();

        IdNameEntity entity = adapter.get(position);
        loadActCode(entity.getId());
    }


    //隐藏勘察话术,平移动画消失
    private void hideAnim() {
        int measuredHeight = rlAct.getMeasuredHeight();
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(rlAct, "translationY", 0, -300 - measuredHeight);
        objectAnimator.setDuration(1000).start();
        rlAct.setVisibility(View.GONE);
    }

    //显示勘察话术，平移动画显示
    private void showAnim() {
        rlAct.setVisibility(View.VISIBLE);
        int measuredHeight = rlAct.getHeight();
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
                rlAct, "translationY", -300 - measuredHeight, 0);
        objectAnimator.setDuration(1000).start();
    }

    private void loadActCode(String id) {
        String url = EAppUtil.getApiHost() + Api.API_ER_ACT_DETAIL;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        HttpUtils.getInstance().post(url, paramMap, new ECallback<ErCodeActEntity>(this) {
            @Override
            public void onSuccess(ErCodeActEntity entity, String s) {

                if (!EmptyUtil.isEmpty(entity.getBackImg()))
                    GlideUtil.into(ErCodeActivity.this, entity.getBackImg(), ivBg);

                flCode.setVisibility(View.VISIBLE);
                tvCode.setText(entity.getActivityName());

                String cardNoUrl = entity.getCardNoUrl();

                Bitmap bitmap = QrCodeUtil.createQRCode(cardNoUrl, 500);
                ivCode.setImageBitmap(bitmap);
            }
        });

    }


}
